package com.gqmk.demo.springcloudproviderhystrix.controller;

import com.gqmk.demo.springcloudapi.domain.Demo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/provider")
public class ProviderController {
    private int count = 0;

    @GetMapping("demo")
    /*
     * 熔断处理（客户端）：指定熔断后的处理方法。
     * 缺点：需要挨个去定义熔断回调
     */
    @HystrixCommand(fallbackMethod = "processHystrix_getDemo")
    public Demo getDemo() {
        count++;
        if (count >= 3) {
            throw new RuntimeException();
        }
        return new Demo().setId(count).setName("aaa").setMoney(new BigDecimal(16500)).setCreateDate(new Date()).setIsEnable(true);
    }

    // 熔断处理方法
    private Demo processHystrix_getDemo() {
        return new Demo().setId(-1).setName("error").setMoney(BigDecimal.ZERO).setCreateDate(new Date()).setIsEnable(false);
    }
}
